﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

public partial class ProductManage_RFProductManage_Manage_VendorManage : System.Web.UI.Page
{
    private Vendor ven;

    public ProductManage_RFProductManage_Manage_VendorManage()
    {
        ven = new Vendor();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindVendors();
        }
    }

    protected void BindVendors()
    {
        this.GridView1.DataSource = ven.LoadEntities();
        this.GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindVendors();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //CustomerInfo customerInfo = new CustomerInfo(GridView1.DataKeys[e.RowIndex].Values[0].ToString(), GridView1.DataKeys[e.RowIndex].Values[1].ToString());
        string vendorId = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        ven.Delete(vendorId);
        BindVendors();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        string vendorID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        string vendorName = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
        VendorInfo vendorInfo = new VendorInfo() { VendorID = vendorID, VendorName = vendorName };
        try
        {
            ven.Insert(vendorInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindVendors();
        }
        catch(Exception ex)
        {
            if (ex.Message == "The VendorID is already exist")
            {
                string str1 = "供应商代码已存在！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            }
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindVendors();
    }
}
